using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Editors;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CustomProject.CreateCustomProject
{
	/// <summary>
    /// Custom page for CreateCustomProject recipe
	/// </summary>
	public partial class CreateCustomProjectCP2 : WizardPageBase
	{
		/// <summary>
        /// Initializes a new instance of the <see cref="CreateCustomProjectCP2"/> class.
		/// </summary>
        public CreateCustomProjectCP2()
		{
		}

		/// <summary>
        /// Initializes a new instance of the <see cref="CreateCustomProjectCP2"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public CreateCustomProjectCP2(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
			SetControls();
		}

		/// <summary>
		/// Gets or sets the bitmap destination folder.
		/// </summary>
		/// <value>The bitmap destination folder.</value>
		[RecipeArgument]
        public ProjectItem BitmapDestinationFolder
		{
			get { return (ProjectItem)veResourcesFolder.Value; }
			set
			{
				if(value != null)
				{					
					veResourcesFolder.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the icon file.
		/// </summary>
        /// <value>The name of the icon file.</value>
		[RecipeArgument]
        public string SourceIconFileName
		{
			get { return (string)veProjectIcon.Value; }
			set
			{
				if(value != null)
				{
					veProjectIcon.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veResourcesFolder.BeginInit();
					this.veResourcesFolder.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResourcesFolder);
					SolutionPickerEditor solutionPickerEditor1 = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes1 = new StringDictionary();
					solutionPickerEditorAttributes1.Add("UnboundReferenceType", "VSSDKAssist.Assets.References.VisualStudio.ProjectFolderReference, VSSDKAssist.Assets");
					solutionPickerEditor1.Configure(solutionPickerEditorAttributes1);
					this.veResourcesFolder.EditorInstance = solutionPickerEditor1;
					this.veResourcesFolder.ConverterInstance = new ProjectItemConverter();
					this.veResourcesFolder.EndInit();

					this.veProjectIcon.BeginInit();
					this.veProjectIcon.ValueType = typeof(string);
					base.Site.Container.Add(veProjectIcon);
					FileChooserEditor fileChooserEditor = new FileChooserEditor();
					StringDictionary fileChooserEditorAttributes = new StringDictionary();
					fileChooserEditorAttributes.Add("Title", Properties.Resources.IconChooserTitle);
					fileChooserEditorAttributes.Add("Filter", Properties.Resources.IconChooserFilter);
					fileChooserEditor.Configure(fileChooserEditorAttributes);
					this.veProjectIcon.EditorInstance = fileChooserEditor;
					this.veProjectIcon.EndInit();
				}
			}
		}

		private void SetControls()
		{
			pctIconPreview.ImageLocation = string.Empty;
		}

		private void veResourcesFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem folder = (ProjectItem)veResourcesFolder.Value;
            DictionaryService.SetValue("BitmapDestinationFolder", folder);
		}

        private void veProjectIcon_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            string bitmapFileName = (string)veProjectIcon.Value;
            DictionaryService.SetValue("SourceIconFileName", bitmapFileName);
            pctIconPreview.ImageLocation = bitmapFileName;
        }

        private void pctIcon_Click(object sender, System.EventArgs e)
        {
            Bitmap bitmap = CustomPageHelper.GetBitmapResource("Project");

            if(bitmap != null)
            {
                InfoForm infoForm = new InfoForm(bitmap);

                infoForm.ShowDialog();
            }
        }
	}
}